import AddCate from "./components/AddCate"
import { notFound } from "next/navigation"
import { Card, CardBody} from "@nextui-org/react";
import { ListboxWrapper } from "./components/ListboxWrapper";

// 公共路径
const baseURL = "http://localhost:3000"

const Cate = async ({ params }: { params: { id: string } }) => {
    const { id } = params

    // 获取目录列表
    const data = await fetch(`${baseURL}/cate`).then(res => res.json())

    const { content } = await fetch(`${baseURL}/content/${id}`).then(async res => {

        if (res.status !== 200) {
            notFound()
        } else {
            return res.json()
        }
    })

    return (
        <>
            <div className="flex justify-center"><AddCate ></AddCate></div>

            <div>{content.content}</div>

            <div className="flex justify-between w-[800px] mx-auto">
                {/* 导航栏 */}
                <ListboxWrapper data={data}></ListboxWrapper>

                {/* 内容区 */}
                <Card className="w-[70%]">
                    <CardBody>
                        <p>{content}</p>
                    </CardBody>
                </Card>
            </div>
        </>
    )
}

export default Cate

// 根据路径生成静态页面
export async function generateStaticParams() {
    const data = await fetch(`${baseURL}/cate`).then(res => res.json())

    return data.map((p: HelpItem) => ({ id: p.id }))
}

// 配置更新时间
export const revalidate = 100